package org.road0329.dlt698.axdr;

import org.road0329.dlt698.ReverseByteArrayInputStream;
import org.road0329.dlt698.ReverseByteArrayOutputStream;
import org.road0329.dlt698.asn1.axdr.AxdrEnum;
import org.road0329.dlt698.asn1.axdr.AxdrType;

public class GetResponse extends AxdrType {
    private byte[] dataCode = null;
    private Choice choice = Choice._ERR_NONE_SELECTED;
    private GetResponseNormal getResponseNormal = null;
    private GetResponseNormalList getResponseNormalList = null;
    private GetResponseRecord getResponseRecord = null;
    private GetResponseRecordList getResponseRecordList = null;
    private GetResponseNext getResponseNext = null;

    public GetResponse() {
    }

    public byte[] getDataCode() {
        return dataCode;
    }

    public GetResponseNormal getGetResponseNormal() {
        return getResponseNormal;
    }

    public GetResponseNormalList getGetResponseNormalList() {
        return getResponseNormalList;
    }

    public GetResponseRecord getGetResponseRecord() {
        return getResponseRecord;
    }

    public GetResponseRecordList getGetResponseRecordList() {
        return getResponseRecordList;
    }

    public GetResponseNext getGetResponseNext() {
        return getResponseNext;
    }

    public void setDataCode(byte[] dataCode) {
        this.dataCode = dataCode;
    }

    public void setGetResponseNormal(GetResponseNormal getResponseNormal) {
        resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL;
        this.getResponseNormal = getResponseNormal;
    }

    public void setGetResponseNormalList(GetResponseNormalList getResponseNormalList) {
        resetChoices();
        this.choice = Choice.GET_RESPONSE_NORMAL_LIST;
        this.getResponseNormalList = getResponseNormalList;
    }

    public void setGetResponseRecord(GetResponseRecord getResponseRecord) {
        resetChoices();
        this.choice = Choice.GET_RESPONSE_RECORD;
        this.getResponseRecord = getResponseRecord;
    }

    public void setGetResponseRecordList(GetResponseRecordList getResponseRecordList) {
        resetChoices();
        this.choice = Choice.GET_RESPONSE_RECORD_LIST;
        this.getResponseRecordList = getResponseRecordList;
    }

    public void setGetResponseNext(GetResponseNext getResponseNext) {
        resetChoices();
        this.choice = Choice.GET_RESPONSE_NEXT;
        this.getResponseNext = getResponseNext;
    }

    public int decode(ReverseByteArrayInputStream input) throws Exception {
        int codeLength = 0;
        AxdrEnum choosen = new AxdrEnum();
        choosen.setConst();
        codeLength += choosen.decode(input);
        resetChoices();
        this.choice = Choice.getEnumByValue((int)choosen.getValue());
        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            this.getResponseNormal = new GetResponseNormal();
            codeLength += this.getResponseNormal.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            this.getResponseNormalList = new GetResponseNormalList();
            codeLength += this.getResponseNormalList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            this.getResponseRecord = new GetResponseRecord();
            codeLength += this.getResponseRecord.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            this.getResponseRecordList = new GetResponseRecordList();
            codeLength += this.getResponseRecordList.decode(input);
            return codeLength;
        } else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            this.getResponseNext = new GetResponseNext();
            codeLength += this.getResponseNext.decode(input);
            return codeLength;
        }
        throw new Exception("Error decoding AxdrChoice: Identifier matched to no item.");
    }

    public int encode(ReverseByteArrayOutputStream output) throws Exception {
        if (this.dataCode != null) {
            byte[] dataCodeArray = this.dataCode;
            for (int i = dataCodeArray.length - 1; i >= 0; i--) {
                output.write(dataCodeArray[i]);
            }
            return dataCodeArray.length;
        }
        if (this.choice == Choice._ERR_NONE_SELECTED) {
            throw new Exception("Error encoding AxdrChoice: No item in choice was selected.");
        }
        int codeLength = 0;
        if (this.choice == Choice.GET_RESPONSE_NEXT) {
            if (this.getResponseNext != null) {
                codeLength += this.getResponseNext.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(5);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getResponseNext is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            if (this.getResponseRecordList != null) {
                codeLength += this.getResponseRecordList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(4);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getResponseRecordList is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            if (this.getResponseRecord != null) {
                codeLength += this.getResponseRecord.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(3);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getResponseRecord is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            if (this.getResponseNormalList != null) {
                codeLength += this.getResponseNormalList.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(2);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getResponseNormalList is null");
            }
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if (this.getResponseNormal != null) {
                codeLength += this.getResponseNormal.encode(output);
                AxdrEnum c = new AxdrEnum();
                c.setMinMaxVal(1);
                codeLength += c.encode(output);
                return codeLength;
            } else {
                throw new Exception("getResponseNormal is null");
            }
        }
        throw new Exception("Error encoding AxdrChoice: No item in choice was encoded.");
    }

    public void encodeAndSave(int encodingSizeGuess) throws Exception {
        ReverseByteArrayOutputStream revOStream = new ReverseByteArrayOutputStream();
        revOStream.setBufSize(encodingSizeGuess);
        encode(revOStream);
        this.dataCode = revOStream.getArray();
    }

    public Choice getChoiceIndex() {
        return this.choice;
    }

    public void resetChoices() {
        this.choice = Choice._ERR_NONE_SELECTED;
        this.getResponseNormal = null;
        this.getResponseNormalList = null;
        this.getResponseRecord = null;
        this.getResponseRecordList = null;
        this.getResponseNext = null;
    }

    public String toString() {
        if (this.choice == Choice.GET_RESPONSE_NORMAL) {
            if (this.getResponseNormal != null) {
                return "choice: {getResponseNormal: " + this.getResponseNormal + "}";
            } else {
                return "choice is getResponseNormal but getResponseNormal is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_NORMAL_LIST) {
            if (this.getResponseNormalList != null) {
                return "choice: {getResponseNormalList: " + this.getResponseNormalList + "}";
            } else {
                return "choice is getResponseNormalList but getResponseNormalList is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD) {
            if (this.getResponseRecord != null) {
                return "choice: {getResponseRecord: " + this.getResponseRecord + "}";
            } else {
                return "choice is getResponseRecord but getResponseRecord is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_RECORD_LIST) {
            if (this.getResponseRecordList != null) {
                return "choice: {getResponseRecordList: " + this.getResponseRecordList + "}";
            } else {
                return "choice is getResponseRecordList but getResponseRecordList is null";
            }
        } else if (this.choice == Choice.GET_RESPONSE_NEXT) {
            if (this.getResponseNext != null) {
                return "choice: {getResponseNext: " + this.getResponseNext + "}";
            } else {
                return "choice is getResponseNext but getResponseNext is null";
            }
        } else {
            return "unknown";
        }
    }

    public enum Choice {
        _ERR_NONE_SELECTED(-1),
        GET_RESPONSE_NORMAL(1),
        GET_RESPONSE_NORMAL_LIST(2),
        GET_RESPONSE_RECORD(3),
        GET_RESPONSE_RECORD_LIST(4),
        GET_RESPONSE_NEXT(5);

        private final int value;

        private Choice(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public static Choice getEnumByValue(int value) {
            for (Choice item : Choice.values()) {
                if (value == item.getValue()) {
                    return item;
                }
            }
            return null;
        }
    }
}



